package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.MarketDeliveryOrder
import com.zxy.supplier_system.server.repository.QueryGoodsUnitResponse
import com.zxy.supplier_system.server.service.MarketDeliveryOrderService
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.LongIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime
import java.time.Period


@RestController
@RequestMapping("markets/deliveryOrders")
class MarketDeliveryOrderController(
    private val marketDeliveryOrderService: MarketDeliveryOrderService
) {

    data class QueryMarketDeliveryOrderRequest(
        val marketIds: List<Int> = emptyList(),
        val startCreatedDate: LocalDate?,
        val endCreatedDate: LocalDate?,
        val validatedStartDate: LocalDate?,
        val validatedEndDate: LocalDate?,
        val status: MarketDeliveryOrder.Status?,
        val number:String?,
    )

    @HasAuthority(Authority.卖场_送货单_查询)
    @GetMapping
    fun queryMarketDeliveryOrders(
        request: QueryMarketDeliveryOrderRequest,
        pageable: Pageable
    ): Page<QueryMarketDeliveryOrderResponse> {
        return this.marketDeliveryOrderService.queryMarketDeliveryOrders(request, pageable)
    }

    data class QueryMarketDeliveryOrderResponse(
        val id: Int,
        val validatedDateTime: OffsetDateTime?,
        val createdDateTime: OffsetDateTime,
        val market: IntIdAndName,
        val goodsKindAmount: Int,
        val status: MarketDeliveryOrder.Status,
        val number: String,
    )

    @HasAuthority(Authority.卖场_送货单_查询)
    @GetMapping("{id}")
    fun getMarketDeliveryOrderDetail(
        @PathVariable id: Int,
        includeItems: Boolean = false,
        includeComparable: Boolean = false
    ): MarketDeliveryOrderDetail {
        return this.marketDeliveryOrderService.getMarketDeliveryOrderDetail(id, includeItems, includeComparable)
    }


    data class MarketDeliveryOrderDetail(
        val id: Int,
        val createdDateTime: OffsetDateTime,
        val market: IntIdAndName,
        val items: List<MarketDeliveryOrderItemResponse>,
        val validatedDateTime: OffsetDateTime?,
        val hasDifference: Boolean,
        val auditedDateTime:OffsetDateTime?,
        val number: String,
    )

    data class MarketDeliveryOrderItemResponse(
        val id: Int,
        val name: String,
        val barcode: String,
        val price: BigDecimal,
        val amount: Int,
        val brand: IntIdAndName?,
        val category: IntIdAndName?,
        val factory:IntIdAndName?,
        val amountInOrder: Int,
        val boxSpecification:String?,
        val unit: LongIdAndName?,
        val units:List<QueryGoodsUnitResponse>,
        val registrationCertificateNumber: String?,
        val recordFilingNumber: String?,
        val expressionPeriod:Period?,
    )

    @HasAuthority(Authority.卖场_送货单_验收)
    @PatchMapping("{id}/validate")
    fun validate(@PathVariable id: Int) {
        this.marketDeliveryOrderService.validate(id)
    }

    @HasAllAuthority(Authority.卖场_送货单_查询,Authority.卖场_订单_查询)
    @GetMapping("{id}/compareWithOriginalOrder")
    fun compareWithOriginalOrder(@PathVariable id: Int): MarketDeliveryOrderCompareWithOriginalOrderResponse {
        return this.marketDeliveryOrderService.compareWithOriginalOrder(id)
    }

    data class MarketDeliveryOrderCompareWithOriginalOrderResponse(
        val number: String,
        val marketId: Int,
        val marketName:String,
        val items:List<MarketDeliveryOrderCompareWithOriginalOrderItem>
    )

    data class MarketDeliveryOrderCompareWithOriginalOrderItem(
        val goodsId: Int,
        val goodsName: String,
        val goodsBarcode: String,
        val amount: Int?,
        val unit:QueryGoodsUnitResponse?,
        val actuallyAmount: Int?,
        val actuallyUnit:QueryGoodsUnitResponse?,
    )


    @HasAuthority(Authority.卖场_送货单_审核)
    @PatchMapping("{id}/audit")
    fun audit(@PathVariable id: Int) {
        this.marketDeliveryOrderService.audit(id)
    }

}
